package com.hedl.classroom.controller;

import com.hedl.classroom.base.exception.CommonResult;
import com.hedl.classroom.base.exception.OnlinecClassroomException;
import com.hedl.classroom.model.po.MediaFiles;
import com.hedl.classroom.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * ProjectName: onlinec-classroom
 * DateTime: 2023/7/23 17:36
 * Author: Big Watermelon
 * Description: 媒资文件管理接口
 */
@Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
@Slf4j
@RestController
@RequestMapping("/open")
public class MediaOpenController {

    @Resource
    private MediaFileService mediaFileService;

    @GetMapping("/preview/{mediaId}")
    @ApiOperation("预览文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mediaId", value = "媒资文件ID", required = true, dataType = "String")
    })
    public CommonResult<String> getPlayUrlByMediaId(@PathVariable("mediaId") String mediaId) {
        MediaFiles mediaFiles = mediaFileService.getFileById(mediaId);
        if (mediaFiles == null || StringUtils.isEmpty(mediaFiles.getUrl())){
            OnlinecClassroomException.cast("视频还没有转码处理！");
        }
        return CommonResult.success(mediaFiles.getUrl());
    }
}
